package com.android.lovely.aav.discount

import android.content.Context
import com.android.lovely.ext.downloadFile
import java.io.File
import java.io.FileNotFoundException

class FileDownloader(private val appContext: Context) {

    suspend fun downloadFile(url: String, fileName: String): File? {
        if (url.isEmpty() || fileName.isEmpty()) return null
        val file = File(appContext.filesDir, fileName)
        if (file.exists() && file.length() > 0) return file

        val tempFile = "${file.absolutePath}.temp"
        if (!url.downloadFile(tempFile)) throw FileNotFoundException()
        File(tempFile).renameTo(file)
        return file
    }
}